﻿using System;
using System.Management;

namespace CatClient.Util
{
    public static class AppEnv
    {
        private static object _cpucoreLocker = new object();

        private static int? _cpuCoreCount = null;

        public static string IP
        {
            get
            {
                return AppEnv.GetLocalIP();
            }
        }

        private static string GetLocalIP()
        {
            string result;
            try
            {
                result = NetworkInterfaceManager.GetLocalHostAddress();
            }
            catch
            {
                result = string.Empty;
            }
            return result;
        }

        internal static int GetCpuCoreCount()
        {
            if (!AppEnv._cpuCoreCount.HasValue)
            {
                object cpucoreLocker = AppEnv._cpucoreLocker;
                lock (cpucoreLocker)
                {
                    if (!AppEnv._cpuCoreCount.HasValue)
                    {
                        int num = 0;
                        foreach (ManagementBaseObject current in new ManagementObjectSearcher("Select * from Win32_Processor").Get())
                        {
                            num += int.Parse(current["NumberOfCores"].ToString());
                        }
                        AppEnv._cpuCoreCount = new int?(num);
                    }
                }
            }
            return AppEnv._cpuCoreCount.Value;
        }
    }
}
